import * as api from '@/api/sourcing/purchaseDetail'
import { MessageBox } from 'element-ui'

const state = {
  // 报价信息 - 供应商信息
  supplierList: [],
  quotationLoading: false,
  // 报价信息 - 基础信息
  basicInfo: {},
  // 报价信息 - 商品表格信息
  goodsList: [],
  // 报价信息 - 报价详情
  sourcingSkuQuoteList: [],
  sourcingSupplierDto: {},
  taxRate2List: [],
  isMulttaxRate: false,
  allSkuTotalPrice: '', // 合计金额小写
  allSkuTotalPriceStr: '', // 合计金额大写
  addQuotationLoading: false,
  tmpQuotationLoading: false,
  computingCostsLoading: false, // 计算成本
  // 商品资质列表
  qualificationList: [],
  // 保存项目信息loading
  saveProjectLoading: false,
  supplierTotal: 0,
  // 报价信息 - all供应商信息
  allSupplierInfo: [],
  allSupplierInfoLoading: false,
  calibrationUserInfo: {},
  // 定标信息
  calibrationList: [],
  calibrationSupplierList: [],
  invoiceTypeList: [], // 发票类型
  taxRateList: [], // 税率
  freightBearerList: [], // 运费承担方
  submitQuotationLoading: false, // 提交报价loading
  completeInquiryLoading: false, // 询价完成loading
  completeCalibrationLoading: false, // 定标完成loading
  plusSizeLoading: false, // 商品信息维护loading
  addSupplierLoading: false, // 添加供应商loading
  calibrationTableLoading: false, // 定标表格loading
  onCalibrateStepTwoLoading: false, // 定标第二步loading
}

const mutations = {
  // 报价信息 - 基础信息、供应商信息、商品信息
  SET_QUOTATION_INFO: (state, result) => {
    const { supplierList, base, skuList } = result;
    state.supplierList = supplierList;
    state.basicInfo = base;
    state.goodsList = skuList;
  },
  // 发票类型
  SET_INVOICE_TYPE: (state, result) => {
    state.invoiceTypeList = result;
  },
  // 税率
  SET_TAX_RATE: (state, result) => {
    state.taxRateList = result;
  },
  // 运费承担方
  SET_FREIGHT_BEARER: (state, result) => {
    state.freightBearerList = result;
  },
  // 报价详情 - 报价明细
  SET_QUOTATION_DETAIL: (state, result) => {
    const { sourcingSkuQuoteVoList, sourcingSupplierDto, allSkuTotalPrice, allSkuTotalPriceStr } = result;
    sourcingSkuQuoteVoList.forEach(item => {
      item.selectAllQualification = false
      item.threeC = !!item.threeC
      item.certificate = !!item.certificate
      item.testReport = !!item.testReport
      item.materialList = !!item.materialList
      if (item.assistSkuUnit) {
        item.isSingleMeter = item.skuUnit === item.assistSkuUnit
      } else {
        item.isSingleMeter = true
      }
    })
    state.sourcingSkuQuoteList = sourcingSkuQuoteVoList;
    state.sourcingSupplierDto = sourcingSupplierDto;
    state.allSkuTotalPrice = allSkuTotalPrice;
    state.allSkuTotalPriceStr = allSkuTotalPriceStr;
    state.taxRate2List = sourcingSupplierDto.taxRate2 && [...new Set(sourcingSupplierDto.taxRate2.split(','))];
    if (state.taxRate2List.length > 1) {
      state.isMulttaxRate = true;
    } else {
      state.isMulttaxRate = false;
    }
  },
  // 报价详情 - 运费分摊
  SET_QUOTATION_SHARE: (state, result) => {
    const { skuQuotes, allSkuTotalPrice, allSkuTotalPriceStr } = result;
    state.allSkuTotalPrice = allSkuTotalPrice;
    state.allSkuTotalPriceStr = allSkuTotalPriceStr;
    if (skuQuotes && skuQuotes.length > 0) {
      state.sourcingSkuQuoteList.forEach(item => {
        skuQuotes.forEach(sku => {
          if (item.sourcingSkuQuoteId === sku.sourcingSkuQuoteId) {
            item.sharePrice = sku.sharePrice; // 运费分摊金额
            item.costPrice = sku.costPrice; // 成本价
            item.noTaxCostPrice = sku.noTaxCostPrice // 无税成本价
            item.totalPrice = sku.totalPrice; // 金额小计
            item.skuNum = sku.skuNum; // 数量
            item.unitPrice = sku.unitPrice; // 单价
          }
        })
      })
    }
  },
  // 报价信息 - 可添加供应商信息
  SET_SUPPLIER_INFO: (state, result) => {
    const { data, totalNum } = result;
    state.allSupplierInfo = data;
    state.supplierTotal = totalNum;
  },
  // 报价信息 - 可添加供应商信息
  SET_QUALIFICATION_INFO: (state, result) => {
    state.qualificationList = result;
  },
  // 定标信息 - 定标信息列表
  SET_CALIBRATION_INFO: (state, result) => {
    const { skuList, emergencyOrder, lowCost, targetCompany, targetDate, targetUser } = result.winBiddingDto;
    state.calibrationList = skuList;
    state.calibrationUserInfo = {
      lowCost,
      emergencyOrder,
      targetCompany,
      targetDate,
      targetUser,
    }
  },
  // 定标信息 - 定标信息第一步返回值
  /* supplierList 集合下对象新增字段
  contractName 原文件名
  newContractName 新文件名
  filePath 路径
  fileSize 文件大小(kb) */
  SET_CALIBRATION_STEP_ONE_INFO: (state, result) => {
    result.forEach(item => {
      item.contractName = '';
      item.newContractName = '';
      item.filePath = '';
      item.fileSize = '';
      item.fileList = [];
    })
    state.calibrationSupplierList = result;
  }
}

const actions = {
  // 报价信息 - 询价 - 获取 基础信息 供应商信息 商品信息
  getInqueryQuotationInfo ({ state, commit }, { payload: { data, cb } }) {
    state.quotationLoading = true;
    return new Promise((resolve, reject) => {
      state.quotationLoading = false;
      api.getQuotationInfoService(data).then(res => {
        if (!res) {
          MessageBox.alert('询价单据已删除', {
            callback: () => {
              window.close();
            }
          })
        }
        if (cb) {
          cb(res);
        }
        commit('SET_QUOTATION_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        state.quotationLoading = false;
        reject(error)
      })
    })
  },

  // 报价信息 - 订货 - 获取 基础信息 供应商信息 商品信息
  getOrderQuotationInfo ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getOrderQuotationInfoService(data).then(res => {
        if (!res) {
          MessageBox.alert('询价单据已删除', {
            callback: () => {
              window.close();
            }
          })
        }
        if (cb) {
          cb(res);
        }
        commit('SET_QUOTATION_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 报价信息 - 获取供应商信息
  getSupplierInfo ({ state, commit }, { payload: { data } }) {
    return new Promise((resolve, reject) => {
      state.allSupplierInfoLoading = true;
      api.getSupplierInfoService(data).then(res => {
        state.allSupplierInfoLoading = false;
        commit('SET_SUPPLIER_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        state.allSupplierInfoLoading = false;
        reject(error)
      })
    })
  },

  // 报价信息 - 添加供应商
  addSupplier ({ commit, state }, { payload: { data, cb } }) {
    state.addSupplierLoading = true;
    api.addSupplierService(data).then((res) => {
      state.addSupplierLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.addSupplierLoading = false;
    });
  },

  // 报价信息 - 删除供应商
  delSupplier ({ commit, state }, { payload: { data, cb } }) {
    state.saveProjectLoading = true;
    api.delSupplierService(data).then((res) => {
      state.saveProjectLoading = false;
      if (cb) {
        cb(res);
      }
    });
  },

  // 报价信息 - 获取 报价详情
  getQuotationDetail ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getQuotationDetailService(data).then(res => {
        if (cb) {
          cb(res);
        }
        commit('SET_QUOTATION_DETAIL', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 报价信息 - 获取 发票类型
  getInvoiceType ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getSysDictByTypeService(data).then(res => {
        if (cb) {
          cb(res);
        }
        commit('SET_INVOICE_TYPE', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 报价信息 - 获取 税率字典
  getTaxTate ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getSysDictByTypeService(data).then(res => {
        if (cb) {
          cb(res);
        }
        commit('SET_TAX_RATE', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 报价信息 - 获取 运费承担方
  getFreightBearer ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getSysDictByTypeService(data).then(res => {
        if (cb) {
          cb(res);
        }
        commit('SET_FREIGHT_BEARER', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 报价信息 - 报价详情 - 分摊运费
  shareFreight ({ commit, state }, { payload: { data, cb } }) {
    state.computingCostsLoading = true;
    api.shareFreightService(data).then((res) => {
      state.computingCostsLoading = false;
      if (cb) {
        cb(res);
      }
      commit('SET_QUOTATION_SHARE', res.data)
    }).catch(() => {
      state.computingCostsLoading = false;
    });
  },

  // 报价信息 - 商品信息 - 获取资质
  getGoodsQualifications ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getQualificationService(data).then(res => {
        if (cb) {
          cb(res);
        }
        commit('SET_QUALIFICATION_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 报价信息 - 商品信息 - 删除商品
  deleteGoods ({ commit, state }, { payload: { data, cb } }) {
    // state.addQuotationLoading = true;
    api.delGoodsService(data).then((res) => {
      // state.addQuotationLoading = false;
      if (cb) {
        cb(res);
      }
    });
  },

  // 报价信息 - 商品信息 - 批量删除商品
  deleteAllGoods ({ commit, state }, { payload: { data, cb } }) {
    // state.addQuotationLoading = true;
    api.delAllGoodsService(data).then((res) => {
      // state.addQuotationLoading = false;
      if (cb) {
        cb(res);
      }
    });
  },

  // 报价信息 - 提交报价为单个供应商
  submitQuotationForSupplier ({ commit, state }, { payload: { data, cb } }) {
    state.addQuotationLoading = true;
    api.addQuotationService(data).then((res) => {
      state.addQuotationLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.addQuotationLoading = false;
    });
  },

  // 报价信息 - 提交报价为单个供应商
  submitTemporaryQuotationForSupplier ({ commit, state }, { payload: { data, cb } }) {
    state.tmpQuotationLoading = true;
    api.temporaryQuotationService(data).then((res) => {
      state.tmpQuotationLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.tmpQuotationLoading = false;
    });
  },

  // 报价信息- 提交报价
  submitQuotation ({ commit, state }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      state.submitQuotationLoading = true;
      api.submitQuotationService(data).then(res => {
        state.submitQuotationLoading = false;
        if (cb) {
          cb(res);
        }
        resolve(res.data)
      }).catch(error => {
        state.submitQuotationLoading = false;
        reject(error)
      })
    })
  },

  // 定标信息 - 获取定标信息 - 询价
  getInqueryCalibrationInfo ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      state.calibrationTableLoading = true;
      api.getInqueryCalibrationInfoService(data).then(res => {
        state.calibrationTableLoading = false;
        if (cb) {
          cb(res);
        }
        commit('SET_CALIBRATION_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        state.calibrationTableLoading = false;
        reject(error)
      })
    })
  },

  // 定标信息 - 获取定标信息 - 订货
  getOrderCalibrationInfo ({ state, commit }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      api.getOrderCalibrationInfoService(data).then(res => {
        if (cb) {
          cb(res);
        }
        commit('SET_CALIBRATION_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 定标信息 - 询价完成
  completeInquiry ({ commit, state }, { payload: { data, cb } }) {
    state.completeInquiryLoading = true;
    api.completeInquiryService(data).then((res) => {
      state.completeInquiryLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.completeInquiryLoading = false;
    });
  },

  // 定标信息 - 定标完成 - 第一步
  completeCalibrationStepOne ({ commit, state }, { payload: { data, cb } }) {
    return new Promise((resolve, reject) => {
      state.completeCalibrationLoading = true;
      api.completeCalibrationStep1Service(data).then(res => {
        state.completeCalibrationLoading = false;
        if (cb) {
          cb(res);
        }
        commit('SET_CALIBRATION_STEP_ONE_INFO', res.data)
        resolve(res.data)
      }).catch(error => {
        state.completeCalibrationLoading = false;
        reject(error)
      })
    })
  },

  // 定标信息 - 定标完成 - 第二步
  completeCalibrationStepTwo ({ commit, state }, { payload: { data, cb } }) {
    state.onCalibrateStepTwoLoading = true;
    api.completeCalibrationStep2Service(data).then((res) => {
      state.onCalibrateStepTwoLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.onCalibrateStepTwoLoading = false;
    });
  },
  // 定标信息 - 定标完成 - 第三步
  completeCalibrationStepThird ({ commit, state }, { payload: { data, cb } }) {
    state.onCalibrateStepTwoLoading = true;
    api.completeCalibrationStep3Service(data).then((res) => {
      state.onCalibrateStepTwoLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.onCalibrateStepTwoLoading = false;
    });
  },
  // 定标信息 - 商品信息维护 -临采商品加码
  getOrderNo ({ commit, state }, { payload: { data, cb } }) {
    state.plusSizeLoading = true;
    api.getOrderNoService(data).then((res) => {
      state.plusSizeLoading = false;
      if (cb) {
        cb(res);
      }
    }).catch(() => {
      state.plusSizeLoading = false;
    });
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
